/*
 * Mailer.h
 *
 *  Created on: 14/06/2009
 *      Author: banouz
 */

#ifndef MAILER_H_
#define MAILER_H_

#include "Worker.h"
#include "OSScheduler.h"
#include "MyMessage.h"
#include "msgComperator.h"

#include <iostream>
#include <queue>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
using namespace std;



class Mailer
{
private :
  map<string,TT> _tT;
  map<string, Worker> *_mailMapWork;
  map<int, string> *_idMap; //saves < real id, unique id>
  map<string, vector<string> > *_graph; // map of nods and their connections
  map<string,MyMessage > _mailerMBox; // <ID,vector of unRead Massage

  priority_queue<MyMessage*,vector<MyMessage*>,mc> _outMBox;
  priority_queue<MyMessage*,vector<MyMessage*>,mc> _inMBox;
  bool _isFull;  /* boolean condition to check if the inbox is full  */

  int _scheduler_num;
  int _quanta;

  pthread_t _mailer_thread;

  OSScheduler* _scheduler;

  bool _exit;
protected :


public:

  Mailer();
  virtual ~Mailer();
  Mailer(map<string, Worker> &mailMapWork,map<string, vector<string> > &graph,
      map<int,string > &idMap);
  void sendRT(string toID,map<string, vector<string> > otherRT);
  void sendNewMess();
  bool treMess(void); //Eran
 // bool hasNewMass(int uniqueID);


  void start(int sc_Num,int quanta);

  void run();

  bool isFull(); /* check if inbox is full */

  void setScheduler(int sc_num); /* set the requested scheduler */

  map<string, Worker>* getWorkers();

};

class MailerException
{
	public:
		string	message;
};

#endif /* MAILER_H_ */
